import java.io.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Scanner;

void main() {
    Scanner sc = new Scanner(System.in);
    ArrayList<Book> bookList = new ArrayList<>();
    File file = new File("./books.txt");
    if (file.exists() && file.length() > 0) {
        try (FileInputStream fis = new FileInputStream(file);
             ObjectInputStream ois = new ObjectInputStream(fis)) {
            bookList = (ArrayList<Book>) ois.readObject();
        } catch (Exception e) {
            System.out.println("数据加载失败！");
        }
    }

    OUTER:
    while (true) {
        System.out.println("-----欢迎光临图书馆！-----");
        System.out.println("请选择需要执行操作的序号：");
        System.out.println("1.展示书籍");
        System.out.println("2.上架书籍");
        System.out.println("3.下架书籍");
        System.out.println("4.退出应用");

        int choice = -1;
        if (sc.hasNextInt()) {
            choice = sc.nextInt();
            sc.nextLine();
        } else {
            System.out.println("输入的序号错误，请输入(1-4)的数字序号！");
            sc.next();
        }

        switch (choice) {
            case 1 -> {
                System.out.println("展示书籍>>>>>");
                if (bookList.isEmpty()) {
                    System.out.println("图书馆暂无书籍，请先上架书籍！");
                } else {
                    bookList.sort(Comparator.comparing(Book::getId));
                    for (Book book : bookList) {
                        System.out.printf("书籍的编号是：%d，书籍的名称是：《%s》，书籍的作者是：%s%n", book.getId(), book.getTitle(), book.getAuthor());
                    }
                }
            }

            case 2 -> {
                System.out.println("上架书籍>>>>>");
                int id;

                while (true) {
                    System.out.println("请输入书籍的编号：(返回上一级请输入0！)");
                    String idStr = sc.nextLine().trim();

                    if (idStr.isEmpty()) {
                        System.out.println("书籍编号不允许为空，请重新输入！");
                        continue;
                    }

                    try {
                        id = Integer.parseInt(idStr);

                        if (id == 0) {
                            continue OUTER;
                        }

                        if (id < 0) {
                            System.out.println("输入的编号错误，请输入大于0的数字编号！");
                            continue;
                        }

                        boolean isUnique = true;
                        for (Book book : bookList) {
                            if (book.getId() == id) {
                                isUnique = false;
                                break;
                            }
                        }

                        if (isUnique) {
                            break;
                        } else {
                            System.out.println("书籍编号已存在，请重新输入！");
                        }
                    } catch (NumberFormatException e) {
                        System.out.println("输入的编号错误，请输入数字编号！");
                    }
                }

                String title;
                while (true) {
                    System.out.println("请输入书籍的名称：");
                    title = sc.nextLine().trim();
                    if (title.isEmpty()) {
                        System.out.println("书籍名称不允许为空，请重新输入！");
                    } else {
                        break;
                    }
                }

                String author;
                while (true) {
                    System.out.println("请输入书籍的作者：");
                    author = sc.nextLine().trim();
                    if (author.isEmpty()) {
                        System.out.println("书籍作者不允许为空，请重新输入！");
                    } else {
                        break;
                    }
                }


                Book book = new Book(id, title, author);
                bookList.add(book);
                try (FileOutputStream fos = new FileOutputStream(file);
                     ObjectOutputStream oos = new ObjectOutputStream(fos)) {
                    oos.writeObject(bookList);
                } catch (Exception e) {
                    System.out.println("数据写入失败！");
                }



                System.out.println("书籍上架成功！");
            }

            case 3 -> {
                System.out.println("下架书籍>>>>>");
                if (bookList.isEmpty()) {
                    System.out.println("图书馆暂无书籍，请先上架书籍！");
                } else {
                    int delNo;

                    while (true) {
                        System.out.println("请输入需要下架的书籍的编号：(返回上一级请输入0！)");
                        if (sc.hasNextInt()) {
                            delNo = sc.nextInt();
                            sc.nextLine();

                            if (delNo == 0) {
                                continue OUTER;
                            }

                            if (delNo < 0) {
                                System.out.println("输入的编号错误，请输入大于0的数字编号！");
                                continue;
                            }

                            boolean isExists = false;
                            Iterator<Book> iterator = bookList.iterator();
                            while (iterator.hasNext()) {
                                Book book = iterator.next();
                                if (book.getId() == delNo) {
                                    isExists = true;
                                    iterator.remove();
                                    break;
                                }
                            }

                            try (FileOutputStream fos = new FileOutputStream(file);
                                 ObjectOutputStream oos = new ObjectOutputStream(fos)) {
                                oos.writeObject(bookList);
                            } catch (Exception e) {
                                System.out.println("数据写入失败！");
                            }

                            if (!isExists) {
                                System.out.println("该书籍编号不存在，请重新输入！");
                            } else {
                                System.out.println("书籍下架成功！");
                                break;
                            }
                        } else {
                            System.out.println("输入的编号错误，请输入数字编号！");
                            sc.next();
                        }
                    }
                }
            }

            case 4 -> {
                System.out.println("退出应用>>>>>");
                break OUTER;
            }

            default -> System.out.println("输入的序号错误，请输入(1-4)的数字序号！");
        }
    }
    sc.close();
}